﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SayHere
{
    public partial class Start : Form
    {
        SayHereEntities _sayHereEntities = null;

        public Start()
        {
            InitializeComponent();

            _sayHereEntities = new SayHereEntities();
            PopulateMainTreeView();
        }

        private void PopulateMainTreeView()
        {
            //set static nodes for administration
            DataTreeNode HomeNode = new DataTreeNode(null, 0, "Home");
            treeViewMain.Nodes.Add(HomeNode);

            DataTreeNode TeacherManagementNode = new DataTreeNode(new TeacherManagement(), 0, "Manage Teachers");
            treeViewMain.Nodes.Add(TeacherManagementNode);

            
            _sayHereEntities.Class.ToList().ForEach(delegate(Class c)
            {
                DataTreeNode ClassNode = new DataTreeNode(c, c.ClassID, c.Name);
                int ClassID = c.ClassID;
                var StudentsInClass = from cs in _sayHereEntities.ClassStudent
                                      join s in _sayHereEntities.Student on cs.StudentID equals s.StudentID
                                      where cs.ClassID.Equals(ClassID)
                                      select s;

                StudentsInClass.ToList().ForEach(delegate(Student st)
                {
                    DataTreeNode StudentNode = new DataTreeNode(st, st.StudentID, st.LastName + ", " + st.FirstName);
                    ClassNode.Nodes.Add(StudentNode);
                });
                treeViewMain.Nodes.Add(ClassNode);
            });
         
        }

        private void treeViewMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DisplaySelection((DataTreeNode)e.Node);
           
        }

        private void DisplaySelection(DataTreeNode dataTreeNode)
        {
            if (dataTreeNode.DataObject == null) return;
            var NodeType = dataTreeNode.DataObject.GetType();
            if (NodeType == typeof(Class))
            {
                Class SelectedClass = (Class)dataTreeNode.DataObject;

            }
            else if (NodeType == typeof(Student))
            {
                Student SelectedStudent = (Student)dataTreeNode.DataObject;
                ListViewItem StudentListItem = new ListViewItem(SelectedStudent.LastName + ", " + SelectedStudent.FirstName);
                ListViewItem.ListViewSubItem StudentSubItem = new ListViewItem.ListViewSubItem(StudentListItem, SelectedStudent.SSN);
                StudentListItem.SubItems.Add(StudentSubItem);
                var StudentAttendance = from a in _sayHereEntities.Attendance
                                        where a.StudentID.Equals(SelectedStudent.StudentID)
                                        select new { a.Date, a.OnTime, a.Absent, a.Reason };
                
               // dataGridViewDetail.DataSource = StudentAttendance;
            }
            else if (NodeType.BaseType == typeof(UserControl))
            {
                UserControl ControlObject = (UserControl)dataTreeNode.DataObject;
                ControlObject.Dock = DockStyle.Fill;

                splitContainerMain.Panel2.Controls.Add(ControlObject);

            }

        }

    }
}
